package com.ruoyi.project.notice.manager.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.notice.manager.domain.NoticeManager;
import com.ruoyi.project.notice.manager.service.NoticeManagerService;
import com.ruoyi.project.notice.type.domain.NoticeType;
import com.ruoyi.project.notice.type.service.NoticeTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/notice/manager")
public class NoticeManagerController extends BaseController {

    @Autowired
    private NoticeManagerService noticeManagerService;

    @Autowired
    private NoticeTypeService noticeTypeService;

    String prefix = "notice/manager";

    @GetMapping()
    public String notice() {
        return prefix + "/manager";
    }

    @PostMapping("list")
    @ResponseBody
    public TableDataInfo managerList(NoticeManager noticeManager) {
        startPage();
        List<NoticeManager> noticeManagers = noticeManagerService.selectAll(noticeManager);
        return getDataTable(noticeManagers);
    }

    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("types",noticeTypeService.findAll());
        return prefix + "/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated NoticeManager noticeManager)
    {
        if (UserConstants.TYPE_NAME_NOT_UNIQUE.equals(noticeManagerService.checkManagerNameUnique(noticeManager)))
        {
            return error("新增公告'" + noticeManager.getTitle() + "'失败，公告名称已存在");
        }

        noticeManager.setBrowse_num(0);
        return toAjax(noticeManagerService.insert(noticeManager));
    }

    /**
     * 修改公告类型
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap, HttpSession session)
    {
        mmap.put("manager", noticeManagerService.selectTypeById(id));
        session.setAttribute("types",noticeTypeService.findAll());
        return prefix + "/edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated NoticeManager noticeManager)
    {

        return toAjax(noticeManagerService.update(noticeManager));
    }


    /**
     * 查看
     * @return
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap){
        mmap.put("manager",noticeManagerService.selectTypeById(id));
        return prefix + "/details";
    }





    /**
     * 校验岗位名称
     */
    @PostMapping("/checkManagerNameUnique")
    @ResponseBody
    public String checkTypeNameUnique(NoticeManager noticeManager)
    {
        return noticeManagerService.checkManagerNameUnique(noticeManager);
    }



}
